//
//  HomeTableViewController.swift
//  XYWeibo
//
//  Created by 常晓璇 on 6/23/15.
//  Copyright © 2015 chang xiaoxuan. All rights reserved.
//

import UIKit
import SVProgressHUD
class HomeTableViewController: BaseTableViewController {

    /// 表格绑定的微博数据数组
    var statusesList:[Status]? {
        didSet {
            // 刷新表格
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        visitorView?.setupViewInfo("visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜", isHome: true)
        
        if !userLogon {
            return
        }
        loadData()
        // 注册可重用cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: "Cell")
//        tableView.estimatedRowHeight = 300
//       tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }

    ///  加载微博主页数据
    private func loadData() {
        Status.loadstatus { (statuses, error) -> () in
            if error != nil {
                SVProgressHUD.showInfoWithStatus("您的网络不给力")
                return
            }
            if statuses == nil {
                print("没有数据")
                return
            }
            self.statusesList = statuses
        }
    }
    
    // MARK: - 表格的数据源方法
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusesList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! StatusCell
        return cell.rowHeight(statusesList![indexPath.row])
//        return 300
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! StatusCell
        cell.status = statusesList![indexPath.row]
        return cell
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        visitorView?.startAnmiation()
    }
    }
